package fgl;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;


/**
 * @author fuguangli
 * @description 前沿类
 * @Create date:    2017/6/30
 */
public class LuceneDemo {
    static  Directory directory = new RAMDirectory();
    static {
        try {
            directory = FSDirectory.open(Paths.get("D:\\Program Files (x86)\\JetBrains\\IDEA WORKSPACE-DEMO\\lucene-demo\\config"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void createIndex() throws IOException {
        File dir = new File("D:\\Program Files (x86)\\JetBrains\\IDEA WORKSPACE-DEMO\\lucene-demo\\file");
        File[] files = dir.listFiles();
        for (File f : files) {
            BufferedReader reader = new BufferedReader(new FileReader(f));
            StringBuilder content = new StringBuilder();
            String chars = "";
            while ((chars = reader.readLine()) != null) {
                content.append(chars).append("\r\n");
            }
            Analyzer analyzer = new StandardAnalyzer();


            IndexWriterConfig config = new IndexWriterConfig(analyzer);
            IndexWriter writer = new IndexWriter(directory, config);

            Document document = new Document();
            document.add(new TextField("filename", f.getName(), Field.Store.YES));
            document.add(new TextField("content", content.toString(), Field.Store.YES));
            document.add(new TextField("path", f.getPath(), Field.Store.YES));

            writer.addDocument(document);
            writer.close();
        }
    }

    public static void testQuery(String text) throws IOException, ParseException {
        //TODO:查询结果没有精确到哪一行哪一列
        Analyzer analyzer = new StandardAnalyzer();

        DirectoryReader directoryReader = DirectoryReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(directoryReader);

        QueryParser parser = new QueryParser("content",analyzer);
        Query query = parser.parse(text);

        ScoreDoc[] scoreDocs = searcher.search(query,1000).scoreDocs;
        for(ScoreDoc sd:scoreDocs){
            Document d = searcher.doc(sd.doc);
            System.out.println("----------------");
            System.out.println("filename="+d.get("filename"));
            System.out.println("content="+d.get("content"));
            System.out.println("path="+d.get("path"));
            System.out.println("----------------");
        }
    }

    public static void main(String[] args) throws IOException, ParseException {
        createIndex();
        testQuery("你");
    }
}
